
// 在 util 包中创建 JsonRW.java
package util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;

import java.io.File;
import java.util.*;

@SuppressWarnings("all")
public class JsonRW {
    public static final File configFile = new File("config.json");
    public static final File historyFile = new File("history.json");

    private JsonRW() {
        throw new AssertionError("不可实例化的对象：JsonRW");
    }

    public static void saveLastUser(String username) {
        set(historyFile, "lastLoginUsername", username);
    }

    public static String getLastUser() {
        try {
            if (isConfigExist()) {
                String content = FileUtil.readUtf8String(configFile);
                Map<String, Object> config = JSONUtil.toBean(content, Map.class);
                return (String) config.get("lastLoginUsername");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object get(File jsonFile, String key) {
        try {
            if (isConfigExist()) {
                String content = FileUtil.readUtf8String(jsonFile);
                Map<String, Object> config = JSONUtil.toBean(content, Map.class);
                return config.get(key);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getStr(File jsonFile, String key) {
        Object o = get(jsonFile, key);
        if (o == null) {
            throw new RuntimeException("键" + key + "不存在");
        }
        return (String) o;
    }

    public static int getInt(File jsonFile, String key) {
        Object o = get(jsonFile, key);
        if (o == null) {
            throw new RuntimeException("键" + key + "不存在");
        }
        return (int) o;
    }

    public static void set(File jsonFile, String key, Object value) {
        Map<String, Object> jsonObject = new HashMap<>();

        // 先读取现有配置
        try {
            if (FileUtil.exist(jsonFile)) {
                String content = FileUtil.readUtf8String(jsonFile);
                if (content != null && !content.isEmpty()) {
                    jsonObject = JSONUtil.toBean(content, Map.class);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 更新指定键值
        jsonObject.put(key, value);

        // 保存完整配置
        try {
            FileUtil.writeUtf8String(JSONUtil.formatJsonStr(JSONUtil.toJsonStr(jsonObject)), jsonFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从配置文件中移除指定的键值对
     * @param key 要移除的键
     */
    public static void remove(File jsonFile, String key) {
        Map<String, Object> jsonObject = new HashMap<>();

        // 先读取现有配置
        try {
            if (FileUtil.exist(jsonFile)) {
                String content = FileUtil.readUtf8String(jsonFile);
                if (content != null && !content.isEmpty()) {
                    jsonObject = JSONUtil.toBean(content, Map.class);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return; // 如果读取失败，直接返回
        }

        // 移除指定键
        if (jsonObject.containsKey(key)) {
            jsonObject.remove(key);

            // 保存更新后的配置
            try {
                FileUtil.writeUtf8String(JSONUtil.formatJsonStr(JSONUtil.toJsonStr(jsonObject)), jsonFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static List<String> getStringList(File jsonFile, String key) {
        try {
            if (FileUtil.exist(jsonFile)) {
                String content = FileUtil.readUtf8String(jsonFile);
                Map<String, Object> config = JSONUtil.toBean(content, Map.class);
                Object value = config.get(key);
                if (value != null) {
                    return JSONUtil.toList(JSONUtil.parseArray(value), String.class);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    public static HashSet<String> getStringSet(File jsonFile, String key) {
        List<String> stringList = getStringList(jsonFile, key);
        return new HashSet<>(stringList);
    }
    
    public static boolean isContains(File jsonFile, String key) {
        return get(jsonFile, key) != null;
    }
    
    private static boolean isConfigExist() {
        return FileUtil.exist(configFile);
    }
}